#!/usr/bin/make -f
# -*- makefile -*-

# Uncomment this to turn on verbose mode.
#export DH_VERBOSE=1

INSTALL=install -Dp

prefix := $(CURDIR)/debian/tmp
bindir := $(prefix)/$(shell /usr/bin/ruby -rrbconfig -e 'puts RbConfig::CONFIG["bindir"]')
libdir := $(prefix)/usr/lib
localstatedir := $(prefix)/var
rubylibdir := $(shell /usr/bin/ruby -rrbconfig -e 'puts RbConfig::CONFIG["vendordir"]')
sysconfdir := $(prefix)/etc
pkgconfdir := $(sysconfdir)/puppet

ifneq (,$(findstring noopt,$(DEB_BUILD_OPTIONS)))
	CFLAGS += -O0
else
	CFLAGS += -O2
endif

configure: configure-stamp
configure-stamp:
	dh_testdir
	touch configure-stamp


build-arch: build
build-indep: build

build: build-stamp
build-stamp: configure-stamp
	dh_testdir
	touch build-stamp

clean:
	dh_testdir
	dh_testroot
	rm -f build-stamp configure-stamp
	rm -f debian/puppet-common.logcheck.ignore.server
	dh_clean

install: build
	dh_testdir
	dh_testroot
	dh_clean -k
	dh_installdirs

	$(CURDIR)/install.rb --destdir=debian/tmp --bindir=/usr/bin --sitelibdir=$(rubylibdir) --ruby=/usr/bin/ruby

	# strip executable bit from all the non-executable files.
	find $(prefix)/$(rubylibdir) -type f -perm /u+x,g+x,o+x -exec chmod a-x {} \;
	# fix the permissions on all of the directories
	find $(prefix)/$(rubylibdir) -type d -exec chmod 755 {} \;

	# Vim auto-syntax-highlighting stuff
	$(INSTALL) -m0644 ext/vim/syntax/puppet.vim				\
		$(CURDIR)/debian/vim-puppet/usr/share/vim/addons/syntax/
	$(INSTALL) -m0644 ext/vim/ftdetect/puppet.vim				\
		$(CURDIR)/debian/vim-puppet/usr/share/vim/addons/ftdetect/
	$(INSTALL) -m0644 ext/vim/indent/puppet.vim				\
		$(CURDIR)/debian/vim-puppet/usr/share/vim/addons/indent/
	$(INSTALL) -m0644 ext/vim/ftplugin/puppet.vim				\
		$(CURDIR)/debian/vim-puppet/usr/share/vim/addons/ftplugin/
	$(INSTALL) -m0644 debian/vim-puppet.yaml				\
		$(CURDIR)/debian/vim-puppet/usr/share/vim/registry/

	# Emacs mode
	$(INSTALL) -m0644 ext/emacs/puppet-mode.el			\
		$(CURDIR)/debian/puppet-el/usr/share/emacs/site-lisp/puppet-mode.el

	# Install the config.ru
	$(INSTALL) -m0644 ext/rack/config.ru \
		$(CURDIR)/debian/puppetmaster-passenger/usr/share/puppet/rack/puppetmasterd
	# Install apache2 site configuration template
	$(INSTALL) -m0644 ext/rack/example-passenger-vhost.conf \
		$(CURDIR)/debian/puppetmaster-passenger/usr/share/puppetmaster-passenger/apache2.site.conf.tmpl

	# Install example environment README
	$(INSTALL) -m0644 ext/README.environment \
		$(CURDIR)/debian/puppet-common/etc/puppet/environments/example_env/README.environment

	# Add ext directory
	cp -pr ext $(CURDIR)/debian/puppet-common/usr/share/puppet
	# Remove misc packaging artifacts not applicable to debian
	rm -rf $(CURDIR)/debian/puppet-common/usr/share/puppet/ext/gentoo
	rm -rf $(CURDIR)/debian/puppet-common/usr/share/puppet/ext/freebsd
	rm -rf $(CURDIR)/debian/puppet-common/usr/share/puppet/ext/solaris
	rm -rf $(CURDIR)/debian/puppet-common/usr/share/puppet/ext/suse
	rm -rf $(CURDIR)/debian/puppet-common/usr/share/puppet/ext/windows
	rm -rf $(CURDIR)/debian/puppet-common/usr/share/puppet/ext/redhat
	rm -rf $(CURDIR)/debian/puppet-common/usr/share/puppet/ext/ips
	rm -rf $(CURDIR)/debian/puppet-common/usr/share/puppet/ext/osx
	rm -rf $(CURDIR)/debian/puppet-common/usr/share/puppet/ext/build_defaults.yaml
	rm -rf $(CURDIR)/debian/puppet-common/usr/share/puppet/ext/project_data.yaml

	dh_installexamples -p puppet-common examples/*

	$(INSTALL) -d -m0775 $(pkgconfdir)/templates
	$(INSTALL) -d -m0775 $(pkgconfdir)/modules

	# Logcheck rules.  Gee I wish you could specify a file to source
	# in dh_installlogcheck.
	ln ext/logcheck/puppet debian/puppet-common.logcheck.ignore.server

# Build architecture-dependent files here.
binary-arch: build install

# Build architecture-independent files here.
binary-indep: build install
	dh_testdir
	dh_testroot
	dh_install -i
	dh_installdocs -i
	dh_installemacsen
	dh_installlogcheck
	dh_installman
	dh_installinit -ppuppetmaster
	dh_installinit -ppuppet --error-handler=true -- defaults 21
	dh_installlogrotate -i
	dh_lintian -i
	dh_compress -i
	dh_fixperms -i
	dh_installdeb -i
	dh_shlibdeps -i
	dh_gencontrol -i
	dh_md5sums -i
	dh_builddeb -i

binary: binary-indep binary-arch
.PHONY: build clean binary-indep binary-arch binary install configure
